package com.jingdianjichi.subject.domain.handler.subject;


import com.jingdianjichi.subject.common.entity.SubjectAnswer;
import com.jingdianjichi.subject.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.subject.common.enums.SubjectTypeEnum;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMultiple;
import com.jingdianjichi.subject.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/*
 * 多选题
 * */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectMultipleService subjectMultipleService;

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //插入多选题目
        List<SubjectMultiple> multipleList = new LinkedList<>();
        List<SubjectAnswer> optionLists = subjectInfoBO.getOptionLists();
        optionLists.forEach(option -> {
            SubjectMultiple subjectMultiple = new SubjectMultiple();
            subjectMultiple.setSubjectId(subjectInfoBO.getId());
            subjectMultiple.setOptionType(Long.valueOf(option.getOptionType()));
            subjectMultiple.setOptionContent(option.getOptionContent());
            subjectMultiple.setIsCorrect(option.getIsCorrect());
            subjectMultiple.setIsDeleted(IsDeletedFlagEnum.UNDELETED.getCode());
            multipleList.add(subjectMultiple);
        });

        subjectMultipleService.batchInsert(multipleList);
    }

    @Override
    public SubjectTypeEnum getHandlerType() {
        return SubjectTypeEnum.MULTIPLE;
    }

    @Override
    public SubjectOptionBO query(int subjectId) {
        SubjectMultiple subjectMultiple = new SubjectMultiple();
        subjectMultiple.setSubjectId(Long.valueOf(subjectId));
        subjectMultiple.setIsDeleted(IsDeletedFlagEnum.UNDELETED.getCode());
        List<SubjectMultiple> subjectMultipleList = subjectMultipleService.queryByCondition(subjectMultiple);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();

        List<SubjectAnswer> subjectAnswerList = new ArrayList<>();

        subjectMultipleList.forEach(subjectMultiple1 -> {
            SubjectAnswer subjectAnswer = new SubjectAnswer();
            subjectAnswer.setOptionContent(subjectMultiple1.getOptionContent());
            subjectAnswer.setOptionType(subjectMultiple1.getOptionType().intValue());
            subjectAnswer.setIsCorrect(subjectMultiple1.getIsCorrect());
            subjectAnswerList.add(subjectAnswer);
        });
        subjectOptionBO.setOptionLists(subjectAnswerList);
        return subjectOptionBO;
    }
}
